<?php

/**
* Controller:  "Add"
*
* This controller allows a logged-in user to add a pet to the database.  Users
* enter all pertinent pet information and upload a picture to the file
* system, then are redirected to the secure page after a successful addition.
* Once added, a pet can be selected from the site front page and deleted from
* the secure section.
*
* Functions:  index
*
* @author     Brandon A Johnson
* @copyright  LIS4368-01: "Web Development with PHP," Spring 2010
* @version    1.0
* @date       April 24, 2010
*
*/

class Add extends Controller {
	
	/**
	* Add() - constructor
	*
	* allows this file to act as a valid CodeIgniter controller
	*/
	function Add()
	{
		parent::Controller();
	}

	/**
	* index() - default function
	*
	* loads all avaiable pets for the "secure" view, then checks to see if
	* data has been sent for processing.  If not, the user is sent back to
	* the secure section to try again.  Is data has been sent, it is validated
	* and processed so it can be added to the database.  The data is then sent 
	* to the database, and a success message is printed if all goes well.
	* Finally, the secure section is loaded again, because the user may want to
	* check on the status of their most recent operation, or delete the entry
	* if there was a mistake.
	*/
	function index()
	{
		// allows the form information to be validated
		$this->load->library('form_validation');

		// loads the "pets" database interaction model
		$this->load->model('pets_model');
		// extracts all pet records currently in the table
		$all_pets = $this->pets_model->list_pets();
		// prepares the data to be sent to the view
		$data = array();
		$data['results'] = $all_pets;
		
		$HTTP_POST_FILES = NULL;
			
		// if no data has been sent for validation, print an error message and reload the views
		if($_POST['name'] == NULL)
		{
			echo "Sorry, but the pet could not be added because he has no name!  Who wants to adopt a pet with no name?...";

			$this->load->view('header');
			$this->load->view('secure', $data);
		}
		// otherwise...
		else
		{
			// sets the validation rules for the "POST" data
			$this->form_validation->set_rules('name', 'Name', 'trim|required');
			$this->form_validation->set_rules('species', 'Species', 'trim');
			$this->form_validation->set_rules('breed', 'Breed', 'trim');
			$this->form_validation->set_rules('description', 'Description', 'trim');
			// sends the data for validation
			$result = $this->form_validation->run();

			// prep each field for insertion into the database
			$name = $_POST['name'];
			$species = $_POST['species'];
			$breed = $_POST['breed'];
			$age = $_POST['age'];
			$weight = $_POST['weight'];
			$description = $_POST['description'];
			$gender = $_POST['gender'];
			$spayedneutered = $_POST['spayedneutered'];

			// if validation was successful...
			if ($result == TRUE)
			{
				// add the new record to the database
				$this->pets_model->add_pet($name, $species, $breed, $age, $weight, $description, $gender, $spayedneutered);
				
				// print a success message
				echo $name . " the " . $breed . " was successfully added!  Let's get this one adopted fast!";		
			}
			// otherwise, print an error message
			else
			{
				$error = "skipped it completely";
				echo ("$error");
			}
		}

		// gets all the pet records, including the new one, and prepares it for the view
		$all_pets = $this->pets_model->list_pets();
		$data['results'] = $all_pets;
		
		// loads the "events" database interaction model
		$this->load->model('events_model');
		// extracts all event records currently in the table
		$all_events = $this->events_model->list_events();
		// prepares the data to be sent to the view
		$data['events'] = $all_events;

		// loads the site header and the secure section, complete with the new pet record
		$this->load->view('header');
		$this->load->view('secure', $data);
	}
}

/* End of file add.php */
/* Location: ./system/application/controllers/add.php */